var $table;  
var selectionIds = [];  //保存选中ids  
var sysShellField = dataObject.sysShellField;
var timeInterval;
$(function () {
	  var columns =[
					{field: 'state', checkbox: true},
					{field: 'serverName' , title: sysShellField.serverName, sortName:'server_name' , sortable: true,halign: 'center',align: 'center'},
					{field: 'ip' , title: sysShellField.ip, sortName:'ip' , sortable: true,halign: 'center',align: 'center'},
					{field: 'userName' , title: sysShellField.userName, sortName:'user_name' , sortable: true,halign: 'center',align: 'center'},
					//{field: 'password' , title: sysShellField.password, sortName:'password' , sortable: true,halign: 'center',align: 'center'},
					{field: 'port' , title: sysShellField.port, sortName:'port' , sortable: true,halign: 'center',align: 'center'},
					//{field: 'cmd' , title: sysShellField.cmd, sortName:'cmd' , sortable: true,halign: 'center',align: 'center'},
					{field: 'type' , title: sysShellField.type,formatter: 'typeFormatter', sortName:'type' , sortable: true,halign: 'center',align: 'center'},
					{field: 'status' , title: sysShellField.status,formatter: 'statusFormatter', sortName:'status' , sortable: true,halign: 'center',align: 'center'},
					{field: 'path' , title: sysShellField.path, sortName:'path' , sortable: true,halign: 'center',align: 'center'},
			    	{field: 'action', title: pageData.action, formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false, width: 150, halign: 'center', align: 'center'}
		         ];
    $table = new BSTable("table", 'sys/sysshell/list', columns,"toolbar",580,'id','id','sysshell');
    var queryParams = function(params) {            
         return {
         	  page: params.pageNumber,
         	  limit: params.pageSize,
         	  sidx: params.sortName,
              order: params.sortOrder,
              serverName: vm.search.serverName 
              //查询字段
             };
     }
	$table.setQueryParams(queryParams);
    $table.init();
});
var typeFormatter = function(value, row, index){
	return value == 1 ? "<span class='label label-warning'>"+sysShellField.iasType+"</span>" :
			"<span class='label label-danger'>"+sysShellField.adsType+"</span>" ;
}
var statusFormatter = function(value, row, index){
	return value == 2 ? "<span class='label label-warning'>"+sysShellField.starting+"</span>" :
			"<span class='label label-danger'>"+sysShellField.ending+"</span>" ;

}
window.actionEvents = {    
	    'click .edit': function (e, value, row, index) {
	    	vm.update(e,row.id);
	    },
	    'click .remove': function (e, value, row, index) {
	    	vm.del(e,row.id);
	    },
	    'click .detail': function (e, value, row, index) {
	    	vm.detail(e,row.id);
	    },
	    'click .cmd': function (e, value, row, index) {
	    	/*window.localStorage.shellId=row.id;
	    	var random = Math.floor(Math.random() * 100000);
	    	window.open("syssshconsole.html?shellId=" + row.id + "_1&random=" + random);*/
	    	vm.checkLog(row.id);
	    },
	    'click .off': function (e, value, row, index) {
	    	window.localStorage.shellId=row.id;
	    	var random = Math.floor(Math.random() * 100000);
	    	window.open("syssshconsole.html?shellId=" + row.id + "_2&random=" + random);
	    },
	    'click .on': function (e, value, row, index) {
	    	window.localStorage.shellId=row.id;
	    	var random = Math.floor(Math.random() * 100000);
	    	window.open("syssshconsole.html?shellId=" + row.id + "_3&random=" + random);
	    }
	    
	};
var actionFormatter = function(value, row, index) {
    var button = [];
	/*if(row.status == 0 || row.status == 3 || row.status == 9){
		if(hasPermission("sysshell:update")) 
			button.push('<a class="edit ml10" href="javascript:void(0)"  data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;');
		if(row.status != 6){
			if(hasPermission("sysshell:delete"))
				button.push('<a class="remove ml10" href="javascript:void(0)" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>&nbsp;&nbsp;');
		}
	}*/
	button.push('<a class="detail ml10" href="javascript:void(0)"  data-toggle="tooltip" title="detail"><i class="glyphicon glyphicon-book"></i></a>&nbsp;&nbsp;');		
	button.push('<a class="cmd ml10" href="javascript:void(0)"  data-toggle="tooltip" title="log"><i class="glyphicon glyphicon-comment"></i></a>&nbsp;&nbsp;');
	if(row.type != 1){
		button.push('<a class="off ml10" href="javascript:void(0)"  data-toggle="tooltip" title="off"><i class="glyphicon glyphicon-off"></i></a>&nbsp;&nbsp;');
		button.push('<a class="on ml10" href="javascript:void(0)"  data-toggle="tooltip" title="on"><i class="glyphicon glyphicon-wrench"></i></a>&nbsp;&nbsp;');
	}
	
	return button.join('');
}

var vm = new Vue({
	el:'#app',
	data:{
		showList: true,
		title: null,
		sysShell: {			
		},
		search:{
			id:null
		}
	},
	methods: {
		monitor: function () {
			defaultActions.assiot("分发系统监控" , "../dialog/monitor_dialog.html",function(dialog){
				clearInterval(timeInterval);
				dialog.close();
			});			
		},
		query: function () {
			defaultActions.query();
		},
		add: function(){
		    vm.sysShell={};
			defaultActions.add(vm);
		},
		update: function (event, id) {
		    defaultActions.update(id, $("#table"),$path+"sys/sysshell/info/", vm, function(r){
	        	vm["sysShell"] = r["sysShell"];
	        });
		    $("#ip").attr("disabled",true); 
		},
		saveOrUpdate: function (event) {
		    $('#defaultForm').bootstrapValidator('validate');
			if($('#defaultForm').data('bootstrapValidator').isValid()){
				 var url = vm.sysShell.id == null || vm.sysShell.id == ''? $path+"sys/sysshell/save" : $path+"sys/sysshell/update";
				 defaultActions.saveOrUpdate(url ,vm, $("#defaultForm"),"sysShell",event);
			}
		},
		del: function (event, id) {
		    defaultActions.del(id, $path+"sys/sysshell/delete", $("#table") ) ;
		},
		audit:function(status){
			defaultActions.audit(status , $path+"sys/sysshell/audit?status="+status, vm);
		},
		reload: function (event) {
		    defaultActions.reload($("#table"),$("#defaultForm"),vm,"sysShell"); 
		},
		detail: function (event, id) {
			defaultActions.detail(vm, id, event);
		},
		checkLog: function(id){
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: sysShellField.logLevel,
				area: ['350px', '170px'],
				shadeClose: false,
				content: jQuery("#logLevelDiv"),
				btn: [pageData.sure],
				btn1: function (index) {
					var random = Math.floor(Math.random() * 100000);
					var level = $("#level").val();
			    	window.open("syssshconsole.html?shellId=" + id + "_1&random=" + random + "&level=" + level);
			    	layer.close(index);
	            }
			});		
		}
	}
});

/*表单校验 start*/
$('#defaultForm').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
	    	serverName: {
	            message: 'The 服务名称 is not valid',
	            validators: {
	                notEmpty: {
	                    message: sysShellField.serverName + pageData.notEmpty
	                },
	                remote:{
	                	message:sysShellField.serverName + pageData.hasExists,
	                	url:$path+'sys/sysshell/checkExist',
	                	type:"get",
	                	dataType:"json",
	                	delay:600,
	                	data:{
	                		name : function () {
	                			return vm.sysShell.serverName;
	                		},
	                		id : function () {
	                			return vm.sysShell.id;
	                		}
	                	}
	                }
	            }
	        },
	    	    				
	    	userName: {
	            message: 'The 服务器用户名 is not valid',
	            validators: {
	                notEmpty: {
	                    message: sysShellField.userName + pageData.notEmpty
	                }
	            }
	        },
	    	    				
	    	password: {
	            message: 'The 密码 is not valid',
	            validators: {
	                notEmpty: {
	                    message: sysShellField.password + pageData.notEmpty
	                }
	            }
	        },
	    	    				
	    	port: {
	            message: 'The ssh端口 is not valid',
	            validators: {
	                notEmpty: {
	                    message: sysShellField.port + pageData.notEmpty
	                },
	                between: {
	                    min: 1,
	                    max: 99999999,
	                    message: "取值区间为1-99999999"
	                },
	                regexp:{
	                	regexp:/^\d+$/,
	                	message:pageData.onlyNum
	                }
	            }
	        },
	    	    				
	    	/*cmd: {
	            message: 'The 启动命令 is not valid',
	            validators: {
	                notEmpty: {
	                    message: sysShellField.cmd + pageData.notEmpty
	                }
	            }
	        }*/
	    	    				
	    	    		    
    }
});
/*表单校验 end*/
